/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Editor_MainGUI;

import B3D_Elements.B3D_Filters.B3D_Filter;
import B3D_Elements.B3D_Lights.B3D_Light;
import B3D_Elements.B3D_Spatials.B3D_Spatial;
import B3D_Elements.Others.B3D_MotionEvent;
import GUI.Dialogs.AdditionalCameraDialog;
import GUI.EditPanes.EditTaskPane;
import GUI.EditPanes.Filter.BasicSSAOTaskPane;
import GUI.EditPanes.Filter.BloomTaskPane;
import GUI.EditPanes.Filter.CartoonEdgeTaskPane;
import GUI.EditPanes.Filter.ColorScaleTaskPane;
import GUI.EditPanes.Filter.CrosshatchTaskPane;
import GUI.EditPanes.Filter.DepthOfFieldTaskPane;
import GUI.EditPanes.Filter.FogTaskPane;
import GUI.EditPanes.Filter.FrostedGlassTaskPane;
import GUI.EditPanes.Filter.LightScatteringTaskPane;
import GUI.EditPanes.Filter.OldFilmTaskPane;
import GUI.EditPanes.Filter.PosterizationTaskPane;
import GUI.EditPanes.Filter.SSAOTaskPane;
import GUI.EditPanes.Filter.ShadowTaskPane;
import GUI.EditPanes.Filter.WaterTaskPane;
import GUI.EditPanes.Light.AmbientLightTaskPane;
import GUI.EditPanes.Light.DirectionalLightTaskPane;
import GUI.EditPanes.Light.PointLightTaskPane;
import GUI.EditPanes.Light.SpotLightTaskPane;
import GUI.EditPanes.Other.AdditionalCameraTaskPane;
import GUI.EditPanes.Other.MotionPathTaskPane;
import GUI.EditPanes.Other.ShadowModeTaskPane;
import GUI.EditPanes.Spatial.Basic3DTaskPane;
import GUI.EditPanes.Spatial.EmitterTaskPane;
import GUI.EditPanes.Spatial.MaterialTaskPane;
import GUI.EditPanes.Spatial.MeshTaskPane;
import GUI.EditPanes.Spatial.PhysicsTaskPane;
import General.CurrentData;
import MonkeyStuff.LightModel;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.effect.ParticleEmitter;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.light.PointLight;
import com.jme3.light.SpotLight;
import com.jme3.post.filters.BloomFilter;
import com.jme3.post.filters.CartoonEdgeFilter;
import com.jme3.post.filters.CrossHatchFilter;
import com.jme3.post.filters.DepthOfFieldFilter;
import com.jme3.post.filters.FogFilter;
import com.jme3.post.filters.LightScatteringFilter;
import com.jme3.post.filters.PosterizationFilter;
import com.jme3.post.ssao.SSAOFilter;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.AbstractShadowFilter;
import com.jme3.shadow.PssmShadowFilter;
import com.jme3.water.WaterFilter;
import com.shaderblow.filter.basicssao.BasicSSAO;
import com.shaderblow.filter.frostedglass.FrostedGlassFilter;
import com.shaderblow.filter.oldfilm.OldFilmFilter;
import java.io.File;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.jdesktop.swingx.VerticalLayout;
import other.ColorScaleFilterWithGetters;

/**
 *
 * @author David
 */
public class EditPane extends JScrollPane
{

    private JPanel panel = new JPanel(new VerticalLayout());
    private EditTaskPane currentEditPane = null;
    Vector<EditTaskPane> taskPanes = new Vector<EditTaskPane>();

    public EditPane()
    {
        setViewportView(panel);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        new Thread(new Runnable()
        {
            public void run()
            {
                while (true)
                {
                    try
                    {
                        Thread.sleep(70);
                    } catch (InterruptedException ex)
                    {
                        Logger.getLogger(EditPane.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    synchronized (taskPanes)
                    {
                        for (EditTaskPane editTaskPane : taskPanes)
                        {
                            editTaskPane.updateData();
                        }
                    }
                }
            }
        }).start();
    }

    /**
     *
     * @param taskPaneContainer
     */
    public void addTaskPane(EditTaskPane taskPaneContainer)
    {
        if (taskPaneContainer instanceof MaterialTaskPane)
        {
            ((MaterialTaskPane) taskPaneContainer).arrange(
                    new File("matD//" + ((Geometry) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject()).getMaterial().getMaterialDef().getAssetName()));
        }
        panel.add(taskPaneContainer);
        currentEditPane = taskPaneContainer;
    }

    public void arrange(boolean arrangeAnyway)
    {
        taskPanes.removeAllElements();
        panel.removeAll();
        if (CurrentData.getEditorWindow().getB3DApplication().getSelectedElement() != null)
        {
            if (CurrentData.getEditorWindow().getB3DApplication().getSelectedElement() instanceof B3D_Spatial)
            {
                Spatial tempSpatial = (Spatial) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject();
                if (tempSpatial.getParent() != null 
                        && !tempSpatial.getParent().equals(CurrentData.getEditorWindow().getB3DApplication().getEditorNode()))
                {
                    Basic3DTaskPane basic3DTaskPane = new Basic3DTaskPane();
                    addTaskPane(basic3DTaskPane);
                    taskPanes.add(basic3DTaskPane);

                    ShadowModeTaskPane shadowModeTaskPane = new ShadowModeTaskPane();
                    addTaskPane(shadowModeTaskPane);
                    taskPanes.add(shadowModeTaskPane);

                    PhysicsTaskPane physicsTaskPane = new PhysicsTaskPane();
                    addTaskPane(physicsTaskPane);
                    taskPanes.add(physicsTaskPane);

                    if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof Geometry)
                    {
                        Geometry tempGeometry = (Geometry) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject();
                        if ((tempGeometry instanceof ParticleEmitter))
                        {

                            EmitterTaskPane emitterTaskPane = new EmitterTaskPane((ParticleEmitter) tempGeometry);
                            addTaskPane(emitterTaskPane);
                            taskPanes.add(emitterTaskPane);

                        }
                        if (!(tempGeometry instanceof ParticleEmitter)
                                && !(tempGeometry.getMesh() instanceof Box)
                                && tempGeometry.getUserData("north") == null)
                        {

                            MeshTaskPane meshTaskPane = new MeshTaskPane(tempGeometry.getMesh());
                            addTaskPane(meshTaskPane);
                            taskPanes.add(meshTaskPane);

                        }
                        if (!(tempGeometry instanceof ParticleEmitter) && tempGeometry.getUserData("north") == null)
                        {

                            MaterialTaskPane materialTaskPane = new MaterialTaskPane(tempGeometry.getMaterial().getMaterialDef().getAssetName());
                            addTaskPane(materialTaskPane);
                            taskPanes.add(materialTaskPane);

                        }
                    }
                } else if (tempSpatial.getName().equals("LightModel"))
                {
                    LightModel tempLightModel = null;
                    for (int i = 0; i < CurrentData.getEditorWindow().getB3DApplication().getLightModels().size(); i++)
                    {
                        if (CurrentData.getEditorWindow().getB3DApplication().getLightModels().get(i).getNode().equals(tempSpatial))
                        {
                            tempLightModel = CurrentData.getEditorWindow().getB3DApplication().getLightModels().get(i);
                        }
                    }
                    if (tempLightModel.getLight() instanceof AmbientLight)
                    {

                        AmbientLightTaskPane ambientLightTaskPane = new AmbientLightTaskPane((AmbientLight) tempLightModel.getLight());
                        addTaskPane(ambientLightTaskPane);
                        taskPanes.add(ambientLightTaskPane);

                    } else if (tempLightModel.getLight() instanceof DirectionalLight)
                    {

                        DirectionalLightTaskPane directionalLightTaskPane = new DirectionalLightTaskPane(tempLightModel);
                        addTaskPane(directionalLightTaskPane);
                        taskPanes.add(directionalLightTaskPane);

                    }
                }
            } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedElement() instanceof B3D_Light)
            {
                LightModel tempLightModel = null;
                Light tempLight = (Light) CurrentData.getEditorWindow().getB3DApplication().getElementList().getObject(CurrentData.getEditorWindow().getB3DApplication().getSelectedElement());
                for (int i = 0; i < CurrentData.getEditorWindow().getB3DApplication().getLightModels().size(); i++)
                {
                    if (CurrentData.getEditorWindow().getB3DApplication().getLightModels().get(i).getLight().equals(tempLight))
                    {
                        tempLightModel = CurrentData.getEditorWindow().getB3DApplication().getLightModels().get(i);
                    }
                }
                if (tempLight instanceof AmbientLight)
                {

                    AmbientLightTaskPane ambientLightTaskPane = new AmbientLightTaskPane((AmbientLight) tempLight);
                    addTaskPane(ambientLightTaskPane);
                    taskPanes.add(ambientLightTaskPane);

                } else if (tempLight instanceof DirectionalLight)
                {

                    DirectionalLightTaskPane directionalLightTaskPane = new DirectionalLightTaskPane(tempLightModel);
                    addTaskPane(directionalLightTaskPane);
                    taskPanes.add(directionalLightTaskPane);

                } else if (tempLight instanceof SpotLight)
                {

                    SpotLightTaskPane spotLightTaskPane = new SpotLightTaskPane(tempLightModel);
                    addTaskPane(spotLightTaskPane);
                    taskPanes.add(spotLightTaskPane);

                } else if (tempLight instanceof PointLight)
                {

                    PointLightTaskPane pointLightTaskPane = new PointLightTaskPane(tempLightModel);
                    addTaskPane(pointLightTaskPane);
                    taskPanes.add(pointLightTaskPane);
                }
            } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedElement() instanceof B3D_Filter)
            {
                if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof BasicSSAO)
                {

                    BasicSSAOTaskPane basicSSAOTaskPane = new BasicSSAOTaskPane((BasicSSAO) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(basicSSAOTaskPane);
                    taskPanes.add(basicSSAOTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof BloomFilter)
                {

                    BloomTaskPane bloomTaskPane = new BloomTaskPane((BloomFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(bloomTaskPane);
                    taskPanes.add(bloomTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof CartoonEdgeFilter)
                {

                    CartoonEdgeTaskPane cartoonEdgeTaskPane = new CartoonEdgeTaskPane((CartoonEdgeFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(cartoonEdgeTaskPane);
                    taskPanes.add(cartoonEdgeTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof ColorScaleFilterWithGetters)
                {

                    ColorScaleTaskPane colorScaleTaskPane = new ColorScaleTaskPane((ColorScaleFilterWithGetters) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(colorScaleTaskPane);
                    taskPanes.add(colorScaleTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof CrossHatchFilter)
                {

                    CrosshatchTaskPane crosshatchTaskPane = new CrosshatchTaskPane((CrossHatchFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(crosshatchTaskPane);
                    taskPanes.add(crosshatchTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof DepthOfFieldFilter)
                {

                    DepthOfFieldTaskPane depthOfFieldTaskPane = new DepthOfFieldTaskPane((DepthOfFieldFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(depthOfFieldTaskPane);
                    taskPanes.add(depthOfFieldTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof FogFilter)
                {

                    FogTaskPane fogTaskPane = new FogTaskPane((FogFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(fogTaskPane);
                    taskPanes.add(fogTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof FrostedGlassFilter)
                {

                    FrostedGlassTaskPane frostedGlassTaskPane = new FrostedGlassTaskPane((FrostedGlassFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(frostedGlassTaskPane);
                    taskPanes.add(frostedGlassTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof LightScatteringFilter)
                {

                    LightScatteringTaskPane lightScatteringTaskPane = new LightScatteringTaskPane((LightScatteringFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(lightScatteringTaskPane);
                    taskPanes.add(lightScatteringTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof OldFilmFilter)
                {

                    OldFilmTaskPane oldFilmTaskPane = new OldFilmTaskPane((OldFilmFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(oldFilmTaskPane);
                    taskPanes.add(oldFilmTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof PosterizationFilter)
                {

                    PosterizationTaskPane posterizationTaskPane = new PosterizationTaskPane((PosterizationFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(posterizationTaskPane);
                    taskPanes.add(posterizationTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof SSAOFilter)
                {

                    SSAOTaskPane sSAOTaskPane = new SSAOTaskPane((SSAOFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(sSAOTaskPane);
                    taskPanes.add(sSAOTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof AbstractShadowFilter)
                {

                    ShadowTaskPane shadowTaskPane = new ShadowTaskPane((AbstractShadowFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(shadowTaskPane);
                    taskPanes.add(shadowTaskPane);

                } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof WaterFilter)
                {

                    WaterTaskPane waterTaskPane = new WaterTaskPane((WaterFilter) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                    addTaskPane(waterTaskPane);
                    taskPanes.add(waterTaskPane);

                }
            } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedElement() instanceof B3D_MotionEvent)
            {
                MotionPathTaskPane motionPathTaskPane = new MotionPathTaskPane((MotionEvent) CurrentData.getEditorWindow().getB3DApplication().getElementList().getObject(CurrentData.getEditorWindow().getB3DApplication().getSelectedElement()), (B3D_MotionEvent) CurrentData.getEditorWindow().getB3DApplication().getSelectedElement());
                addTaskPane(motionPathTaskPane);
                taskPanes.add(motionPathTaskPane);
            }
        } else if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() != null)
        {
            if (CurrentData.getEditorWindow().getB3DApplication().getSelectedObject() instanceof AdditionalCameraDialog)
            {
                AdditionalCameraTaskPane additionalCameraTaskPane = new AdditionalCameraTaskPane((AdditionalCameraDialog) CurrentData.getEditorWindow().getB3DApplication().getSelectedObject());
                addTaskPane(additionalCameraTaskPane);
                taskPanes.add(additionalCameraTaskPane);
            }
        }
        validate();
        revalidate();
        repaint();
    }

    /**
     *
     * @return
     */
    public EditTaskPane getCurrentEditPane()
    {
        return currentEditPane;
    }
}
